package com.wn.pettyloan.controller;

import com.wn.pettyloan.entity.Debit;
import com.wn.pettyloan.entity.User;
import com.wn.pettyloan.dto.UserDto;
import com.wn.pettyloan.entity.Userinfo;
import com.wn.pettyloan.service.DebitService;
import com.wn.pettyloan.service.UserService;
import com.wn.pettyloan.service.UserService;
import com.wn.pettyloan.service.UserinfoService;
import com.wn.pettyloan.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.Collection;

/**
 * @program: pettyloan
 * @ClassName UserinfoController
 * @description:
 * @author: meibing
 * @create: 2022-12-07 14:16
 * @Version 1.0
 **/
@RestController
@RequestMapping("/userinfo")
public class UserinfoController {

    @Autowired
    private UserinfoService userinfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private DebitService debitService;


    @PostMapping("/addUserinfo")
    public Result insertUserinfo(@RequestBody Userinfo userinfo){
        //取产品id
        Integer productId = Integer.valueOf(userinfo.getBackup1());
        BigDecimal requestLimit = new BigDecimal(userinfo.getBackup2());

        userinfo.setBackup1(null);
        userinfo.setBackup2(null);
        //判断是否跟用户表关联,所以userId最好从token里拿
        if(userinfo.getUserId() == null)
            return Result.error().data("message","未关联数据UserId");

        int result = userinfoService.insertSelective(userinfo);

        if(result>0){
            //region 这里需要生成借款表
            Debit debit = new Debit();
            debit.setUserId(userinfo.getUserId());
            debit.setProductId(productId);
            debit.setDebitRequestLimit(requestLimit);
            /*审批期数美好*/
            debit.setDebitStatus(10);
            if (debitService.insert(debit)!=1)
                return Result.error().data("message","生成借款表错误");
            //endregion
            return Result.ok();
        }
        else
            return Result.error().data("message","生成用户信息表错误");
    }

    @PostMapping("selectByUser")
    public Result selectByUser(@RequestBody UserDto userDto){
        Userinfo userinfo = userinfoService.selectByUser(userDto);
        return Result.ok().data("userinfo",userinfo);
    }
    @PutMapping("updateUserinfo")
    public Result selectByUser(@RequestBody Userinfo userinfo){
        if(userinfo.getUserinfoId() == null){
            userinfoService.insertSelective(userinfo);
        } else {
            userinfoService.updateByPrimaryKeySelective(userinfo);
        }
        return Result.ok();
    }

    @ApiOperation("根据借款Id查用户提交的详细") //这里没有限制状态
    @PostMapping("/getByDId")
    public Result geiInfoByDebitId(Integer debitId){
//        Integer debitId = Integer.valueOf(debitIdStr);
        Userinfo userinfo = userinfoService.selectByDebitId(debitId);
        return Result.ok().data("userinfo",userinfo);
    }

}